package com.blue.waffle.iwc.screens;

import java.sql.SQLException;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.TextInputListener;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.blue.waffle.iwc.domains.Profile;
import com.blue.waffle.iwc.game.IWCGame;
import com.blue.waffle.iwc.ui.ButtonActorListener;
import com.blue.waffle.iwc.ui.ParallaxBackground;
import com.blue.waffle.iwc.ui.ParallaxLayer;

public abstract class AbstractScreen implements Screen, InputProcessor {

	protected static final float WINDOW_PADDINGS = 60;
	protected static final float CELL_PADDINGS = 5;
	protected  SpriteBatch batch;
	protected  BitmapFont font;
	protected final Stage gameStage;
	protected final IWCGame game;
	private TextureAtlas textureAtlas;
	private Skin uiSkin;
	private static ParallaxBackground parallaxBackground;
	private ParticleEffect particleEffect;
	private InputMultiplexer inputMultiplexer;
	/**
	 * @param game
	 */
	public AbstractScreen(IWCGame game) {
		super();
		this.game=game;
		this.font=new BitmapFont();
		this.batch=new SpriteBatch();
		this.gameStage=new Stage(0,0,true);
		inputMultiplexer=new InputMultiplexer();
		
		Gdx.input.setCatchBackKey(true);
		inputMultiplexer.addProcessor(this);
		inputMultiplexer.addProcessor(gameStage);
		Gdx.input.setInputProcessor(inputMultiplexer);
	}

	@Override
	public void render(float delta) {
		// the following code clears the screen with the given RGB color (white)

		Gdx.gl20.glClearColor(0.2f, 0.8f, 1, 1f);
		Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

		if (parallaxBackground != null) {
			parallaxBackground.render(delta);
		}

		batch.begin();
		draw(delta);
		batch.end();

		gameStage.act(delta);
		gameStage.draw();

		Table.drawDebug(gameStage);
	}
	
	@Override
	public void resize(int width, int height) {
		gameStage.setViewport(width, height, true);
	}
	
	@Override
	public void show() {
		if (parallaxBackground==null) {
			parallaxBackground=(new ParallaxBackground(new ParallaxLayer[]{
					new ParallaxLayer(getTextureAtlas().findRegion("sky"), new Vector2(1f,1f), new Vector2(0,0),new Vector2(0, IWCGame.VIRTUAL_HEIGHT-180))
					
			}, IWCGame.VIRTUAL_WIDTH, IWCGame.VIRTUAL_HEIGHT, new Vector2(20,0)));
		}
	}

	protected void draw(float delta){
		if (particleEffect != null) {
			particleEffect.setPosition(IWCGame.VIRTUAL_WIDTH / 2,
					IWCGame.VIRTUAL_HEIGHT / 2);
			particleEffect.draw(batch, delta);
			if (particleEffect.isComplete()) {
				particleEffect.dispose();
			}
		}
	}
	
	@Override
	public void hide() {
		try {
			game.getGameManager().PersistProfile();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void pause() {
		try {
			game.getGameManager().PersistProfile();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void resume() {
	}

	public TextureAtlas getTextureAtlas() {
		if (game.getAssetManager().getLoadedAssets()==0) {
			return null;
		}
		if (textureAtlas==null) {
			textureAtlas =//new TextureAtlas(Gdx.files.internal("image-atlas/image-atlas.pack"));
					game.getAssetManager().get("image-atlas/image-atlas.atlas", TextureAtlas.class);
		}
		return textureAtlas;
	}
	
	public Skin getUiSkin() {
		if (uiSkin==null) {
			uiSkin= //new Skin(Gdx.files.internal("skins/uiskin.json"));
					game.getAssetManager().get("skins/uiskin.json", Skin.class);
		}
		return uiSkin;
	}
	
	@Override
	public void dispose() {
		gameStage.dispose();
		font.dispose();
        batch.dispose();
        game.getDatabaseHelper().dispose();
        if (particleEffect!=null) {
			particleEffect.dispose();
		}
        
        game.getDatabaseHelper().dispose();
        System.out.println("screen: disposed");
        /*if (textureAtlas!=null) {
        	textureAtlas.dispose();
		}
        
        if (uiSkin!=null) {
			uiSkin.dispose();
		}*/
	}
	
	public ParticleEffect getParticleEffect() {
		return particleEffect;
	}
	
	public void setParticleEffect(ParticleEffect particleEffect) {
		this.particleEffect = particleEffect;
	}

	public SpriteBatch getBatch() {
		return batch;
	}

	public BitmapFont getFont() {
		return font;
	}

	public Stage getGameStage() {
		return gameStage;
	}

	public IWCGame getGame() {
		return game;
	}
	
	public ParallaxBackground getParallaxBackground() {
		return parallaxBackground;
	}

	@Override
	public boolean keyDown(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyUp(int keycode) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchUp(int screenX, int screenY, int pointer, int button) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDragged(int screenX, int screenY, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean mouseMoved(int screenX, int screenY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}
	
	/**
	 * A wrapper method to create a button table that can be customized the
	 * event handling.
	 * 
	 * @param skin
	 * @return
	 */
	protected Table getButtonTable(Skin skin,ButtonActorListener backButtonActorListener) {
		Table table = new Table(skin);
		table.setFillParent(true);
		table.defaults().size(100, 50).padBottom(5).padRight(WINDOW_PADDINGS);
		TextButton backBtn = new TextButton("Back", skin);
		backBtn.addListener(backButtonActorListener);
		table.add(backBtn).right();
		//table.debug();
		return table;

	}
	
	public static class MyTextInputListener implements TextInputListener {
		   @Override
		   public void input (String text) {
		   }

		   @Override
		   public void canceled () {
		   }
		}

}
